package com.course.service.impl;
/*
@author By 林中小鸟
@package  com.self.introduction.service.impl
@create 2021 - 09 - 2021\9\23 0023
@QQ: 357182695
@Em: themooncss@gmail.com
*/

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.course.entity.Permission;
import com.course.entity.Role;
import com.course.mapper.UserMapper;
import com.course.entity.User;
import com.course.service.UserService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public Set<String> getUserRolAsSet(User userLogin) {
        List<Map<String, Object>> userData = this.getBaseMapper().getUserRols(userLogin.getId());
        return userData.stream().map(item->item.get("name").toString()).collect(Collectors.toSet());
    }

    @Override
    public Set<String> getUserPermissionAsSet(User userLogin) {
        List<Permission> userData = this.getBaseMapper().getUserPermissions(userLogin.getId());
        return userData.stream().map(item->item.getValue().toString()).collect(Collectors.toSet());
    }

    @Override
    public Set<Permission> getUserMenuAsSet(User userLogin) {
        List<Permission> userData = this.getBaseMapper().getUserPermissions(userLogin.getId());
        return new HashSet<>(userData);
    }

    @Override
    public boolean linkRoles(Long uid,Collection<Long> roles) {
        //删除关联关系
        unLinkRoles(uid);
        //重新关联数据
        for(Long roleId:roles){
            this.getBaseMapper().linkRole(uid,roleId);
        }
        return false;
    }

    @Override
    public boolean unLinkRoles(Long uid) {
        return this.getBaseMapper().unLinkRole(uid);
    }
}
